import permision from "@/js_sdk/wa-permission/permission.js"
import {
	listView
} from "@/api/index.js"
const TOKEN = 'token'
const ENTERPRISE = 'enterprise'
const KDUSER = 'kduser'
export function getToken() {
	return uni.getStorageSync(TOKEN)
}
export function setToken(token) {
	uni.setStorageSync(TOKEN, token)
}
export function clearToken() {
	uni.removeStorageSync(TOKEN)
}
export function getEnterprise() {
	return uni.getStorageSync(ENTERPRISE)
}
export function setEnterprise(enterprise) {
	uni.setStorageSync(ENTERPRISE, enterprise)
}
export function clearEnterprise() {
	uni.removeStorageSync(ENTERPRISE)
}
export function getUser() {
	return uni.getStorageSync(KDUSER)
}
export function setUser(user) {
	uni.setStorageSync(KDUSER, user)
}
export function clearUser() {
	uni.removeStorageSync(KDUSER)
}
//获取基础资料
export function baseData(formaid, field) {
	return new Promise(resolve => {
		let keys = ''
		if (field) {
			keys = "FNumber,FName" + ',' + field
		} else {
			keys = "FNumber,FName"
		}
		let params = {
			formid: formaid,
			data: {
				"FieldKeys": keys,
				"FormId": formaid,
				"FilterString": `FCreateOrgId='${getUser().kdOrgId}' AND FDocumentStatus='C'`,
			}
		}
		listView(params).then(res => {
			let dataArr = []
			for (let i = 0; i < res.data.length; i++) {
				let obj = {
					title: res.data[i][1],
					id: res.data[i][0],
				}
				if (keys) {
					obj[field] = res.data[i][2]
				}
				dataArr.push(obj)
			}
			resolve(dataArr)
		})
	})

}
//处理返回数据
export function dataMerge(field, data) {
	let list = []
	for (let i = 0; i < data.length; i++) {
		let item = {}
		for (let j = 0; j < field.length; j++) {
			item[field[j]] = data[i][j]
		}
		list.push(item)
	}
	return list
}

//处理带小数点的键值审批
export function objHandle(field) {
	let list = []
	for (let i = 0; i < field.length; i++) {
		if (field[i].indexOf(".") > -1) {
			list.push(field[i].split(".")[1])
		} else {
			list.push(field[i])
		}

	}
	return list
}
//其他处理小数点
export function listHandle(field) {
	let list = []
	for (let i = 0; i < field.length; i++) {
		if (field[i].indexOf(".") > -1) {
			list.push(field[i].replace(".", "_"))
		} else {
			list.push(field[i])
		}

	}
	return list
}
// 时间格式过滤
export function timesTamp(date = new Date()) {
	let dateTime = ''
	let y = date.getFullYear()
	let m = date.getMonth() + 1
	m = m < 10 ? ('0' + m) : m
	let d = date.getDate()
	d = d < 10 ? ('0' + d) : d
	let h = date.getHours()
	h = h < 10 ? ('0' + h) : h
	let M = date.getMinutes()
	M = M < 10 ? ('0' + M) : M
	let s = date.getSeconds()
	s = s < 10 ? ('0' + s) : s
	return dateTime = y + '-' + m + '-' + d;
	// ' + h + ':' + M + ':' + s

}